package main

import (
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
	"time"
)

func timeMiddleWare() gin.HandlerFunc {
	return func(c *gin.Context) {
		begin := time.Now()
		c.Next()
		time.Sleep(1 * time.Second)
		useTime := time.Since(begin)
		log.Printf("use time %s\n", useTime)
	}
}

func timeMiddleWare2() gin.HandlerFunc {
	return func(c *gin.Context) {
		begin := time.Now()
		c.Next()
		time.Sleep(1 * time.Second)
		useTime := time.Since(begin)
		log.Printf("use time %s\n", useTime)
	}
}

func main() {
	engine := gin.Default()
	engine.Use(timeMiddleWare()) //全局中间件

	engine.GET("/", timeMiddleWare2(), func(c *gin.Context) {
		c.String(http.StatusOK, "ok")
	})

	engine.GET("/home", func(c *gin.Context) {
		c.String(http.StatusOK, "ok home")
	})

	engine.Run(":5656")
}
